<?php
/*! \brief
 * This is WFDB class
 * Manipulate all the db handle of Widget Framework
 * \author William Su
 */
require_once (dirname(__FILE__) . '/DB.abstract.php');
class WFDB extends ADB
{
    /*private $objFramework;
    private $objWidgetPoolFactory;
    private $objDataObject;
    public function __construct($objFramework){
        $this->objFramework = $objFramework;
        $this->objWidgetPoolFactory = $this->objFramework->getWidgetPoolFactory();
        $this->objDataObject = $this->objFramework->getWidgetPoolFactory()->getDataObject();
    }
    public function setObjDataObject($objDataObject){
        $this->objDataObject = $objDataObject;
    }*/
    /**
     *\brief Description
     * - initial WF data
     * - serverlist original Data as below:
     * id=1
     * owner=0
     * deleted=0
     * lastcheck=0
     * name=localhost
     * hostinfo={"host":"localhost","type":"modSimple"}
     * - serverlisttype original Data as below:
     * id=1
     * type=modSimple
     * passwordonly=0
     * - WP_UPDATE_LOG original Data as below:
     * log_id=1
     * status=1
     * fail_point=0
     * createtime=now()
     */
    public function initWFDBData(){
        $objParameter = $this->objWidgetPoolFactory->getParameter();
        $objParameter->appendFrom('serverlist');
        $objParameter->setPrimaryKey('id');
        $objParameter->andWhereEqual('id', '1');
        $isExisted = $this->objDataObject->querySelectCountByParameter($objParameter);
        $assocData = array();
        $assocData['owner'] = '0';
        $assocData['deleted'] = '0';
        $assocData['lastcheck'] = '0';
        $assocData['name'] = 'localhost';
        $assocData['hostinfo'] = '{"host":"localhost","type":"modSimple"}';
        $objParameter = $this->objWidgetPoolFactory->getParameter();
        $objParameter->appendFrom('serverlist');
        $objParameter->setAssocData($assocData);
        if(!$isExisted){ //INSERT
            $this->objDataObject->queryInsertByParameter($objParameter);
        } else{ //UPDATE
            $objParameter->andWhereEqual('id', '1');
            $this->objDataObject->queryUpdateByParameter($objParameter);
        }
        $objParameter = $this->objWidgetPoolFactory->getParameter();
        $objParameter->appendFrom('serverlisttype');
        $objParameter->setPrimaryKey('id');
        $objParameter->andWhereEqual('id', '1');
        $isExisted = $this->objDataObject->querySelectCountByParameter($objParameter);
        $assocData = array();
        $assocData['type'] = 'modSimple';
        $assocData['passwordonly'] = '0';
        $objParameter = $this->objWidgetPoolFactory->getParameter();
        $objParameter->appendFrom('serverlisttype');
        $objParameter->setAssocData($assocData);
        if(!$isExisted){ //INSERT
            $this->objDataObject->queryInsertByParameter($objParameter);
        } else{ //UPDATE
            $objParameter->andWhereEqual('id', '1');
            $this->objDataObject->queryUpdateByParameter($objParameter);
        }
        //WidgetPool original data initial
        $this->objFramework->getWidgetPoolFactory()->getWidgetPoolDB()->initWidgetPoolDBData();
    }

    /**
     *\brief Description
     * - This method will initial create All table of Widget Framework
     * - If success return true, If failed will raised Exception
     *\par Variable
     * - EX : $mixTableName string
     * WP_UPDATE_LOG
     * - EX : $mixTableName array
     * array (
     * 0 => 'WIDGET',
     * 1 => 'WIDGET_CATEGORY'
     * )
     */
    public function initWFDBSchema($mixTableName = null){
        $sqlCreateDatabase = $this->objFramework->getDbFactory()->getSqlGen()->getCreateDatabase();
        $isSuccess = true; //default
        if($sqlCreateDatabase){ //Create tmwf database
            //due to tmwf is not exist, so $strDbName='';
            $strDbName = '';
            $isSuccess = $isSuccess && $this->objFramework->getDataObject($strDbName)->query($sqlCreateDatabase);
            // If data tmwf create success, refresh $objDataObject
            $objDataObject = $this->objFramework->getDataObject($this->objFramework->getConfigFactory()->getConfig()->getDbConfigDbname());
            $this->setObjDataObject($objDataObject);
            $this->objWidgetPoolFactory->getWidgetPoolDB()->setObjDataObject($objDataObject);
        }
        $defaultArrTable = $this->objFramework->getConfigFactory()->getOpt()->getWFTableName();
        $strQueryType = 'create';
        $isSuccess = $isSuccess && parent::createDropTruncateDBSchema($defaultArrTable, $strQueryType, $mixTableName);
        $this->initWFDBData();
        return $isSuccess;
    }

    /**
     *\brief Description
     * - This method will drop All table of Widget Framework
     * - If table is existed and execute drop failed, it will raised Exception
     *\par Variable
     * - EX : $mixTableName string
     * WP_UPDATE_LOG
     * - EX : $mixTableName array
     * array (
     * 0 => 'WIDGET',
     * 1 => 'WIDGET_CATEGORY'
     * )
     */
    public function dropWFDBSchema($mixTableName = null){
        $defaultArrTable = $this->objFramework->getConfigFactory()->getOpt()->getWFTableName();
        $strQueryType = 'drop';
        $isSuccess = parent::createDropTruncateDBSchema($defaultArrTable, $strQueryType, $mixTableName);
        return $isSuccess;
    }

    /**
     *\brief Description
     * - This method will truncate All table of Widget Framework
     *\par Variable
     * - EX : $mixTableName string
     * WP_UPDATE_LOG
     * - EX : $mixTableName array
     * array (
     * 0 => 'WIDGET',
     * 1 => 'WIDGET_CATEGORY'
     * )
     */
    public function truncateWFTable($mixTableName = null){
        $defaultArrTable = $this->objFramework->getConfigFactory()->getOpt()->getWFTableName();
        $strQueryType = 'truncate';
        $isSuccess = parent::createDropTruncateDBSchema($defaultArrTable, $strQueryType, $mixTableName);
        return $isSuccess;
    }
}
